package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.FlowElementProcessContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.CustomPropertiesProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowReTimeoutSettingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理节点超时配置信息
 * <p>
 * properties -> timeoutSetting
 *
 * @author lilh
 * @date 2020/11/16 17:09
 */
@Service
public class TimeoutNoticeCustomPropertiesProcessor implements CustomPropertiesProcessor {

    @Autowired
    private FlowReTimeoutSettingRepository flowReTimeoutSettingRepository;

    @Override
    public boolean support(JsonNode elementNode) {
        JsonNode properties = elementNode.get("properties");
        if (nonNull(properties)) {
            return nonNull(properties.get("timeoutSetting"));
        }
        return false;
    }

    @Override
    public void process(FlowElementProcessContext context) {
        JsonNode properties = context.getElementNode().get("properties");
        Map<String,JsonNode> taskUsers=new HashMap<>();
        if (!properties.isNull()) {
            JsonNode candidateUsers = properties.get("candidateUsers");
            if (!candidateUsers.isNull()){
                taskUsers.put(context.getNodeCode(),candidateUsers);
            }
        }
        flowReTimeoutSettingRepository.saveTimeoutNoticeSetting(context.getDeployId(),
                context.getNodeCode(),
                context.getElementNode().get("properties").get("timeoutSetting"),taskUsers);
    }
}
